home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / info / gpc.i3 < prev    next >
Encoding:
GNU Info File  |  2001-02-09  |  50.0 KB  |  1,293 lines

  1. This is gpc.info, produced by makeinfo version 4.0 from gpc.texi.
  2.  
  3. INFO-DIR-SECTION GNU programming tools
  4. START-INFO-DIR-ENTRY
  5. * GPC: (gpc).                   The GNU Pascal Compiler.
  6. END-INFO-DIR-ENTRY
  7. INFO-DIR-SECTION Individual utilities
  8. START-INFO-DIR-ENTRY
  9. * GPC: (gpc)Invoking GPC.       The GNU Pascal Compiler.
  10. END-INFO-DIR-ENTRY
  11.  
  12.    This file documents the GNU Pascal Compiler.
  13.  
  14.    Copyright (C) 1988, 1996-2001 Free Software Foundation, Inc.
  15.  
  16.    Permission is granted to make and distribute verbatim copies of this
  17. manual provided the copyright notice and this permission notice are
  18. preserved on all copies.
  19.  
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the sections entitled "GNU General Public License", "The GNU
  23. Project", "The GNU Manifesto" and "Funding for Free Software" are
  24. included exactly as in the original, and provided that the entire
  25. resulting derived work is distributed under the terms of a permission
  26. notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that the sections entitled "GNU General Public
  31. License", "The GNU Project", "The GNU Manifesto" and "Funding for Free
  32. Software" and this permission notice, may be included in translations
  33. approved by the Free Software Foundation instead of in the original
  34. English.
  35.  
  36. 
  37. File: gpc.info,  Node: MS Windows 95/98/NT,  Prev: MS-DOS or OS/2 with EMX,  Up: Compilation Notes
  38.  
  39. MS Windows 95/98/NT
  40. -------------------
  41.  
  42.    There are two ports of the GNU development tools to MS Windows
  43. 95/98/NT: CygWin and mingw32.
  44.  
  45.    The CygWin environment implements a POSIX layer under MS Windows,
  46. giving it large parts of the functionality of Unix. Thus, compiling GCC
  47. and GPC under the CygWin environment can be done following the
  48. instructions for compiling it under a Unix-like system (see *Note
  49. Compiling GPC::).
  50.  
  51.    The Minimalists' GNU Win32 environment, mingw32, uses the native
  52. `crtdll.dll' library of MS Winodws. It is much smaller than CygWin, but
  53. it is not self-hosting and must be crossbuilt from another system (see
  54. *Note Crossbuilding::).
  55.  
  56. 
  57. File: gpc.info,  Node: Cross-Compilers,  Next: Crossbuilding,  Prev: Compilation Notes,  Up: Installation
  58.  
  59. Building and Installing a cross-compiler
  60. ========================================
  61.  
  62.    GNU Pascal can function as a cross-compiler for many machines.
  63. Information about GNU tools in a cross-configuration can be found at
  64. `ftp://ftp.cygnus.com/pub/embedded/crossgcc/'.
  65.  
  66.    Since GNU Pascal generates assembler code, you need a cross-assembler
  67. that GNU Pascal can run, in order to produce object files. If you want
  68. to link on other than the target machine, you need a cross-linker as
  69. well. It is straightforward to install the GNU binutils to act as
  70. cross-tools - see the installation instructions of the GNU binutils for
  71. details.
  72.  
  73.    You also need header files and libraries suitable for the target
  74. machine that you can install on the host machine. Please install them
  75. under `PREFIX/PLATFORM/include/', for instance
  76. `/usr/local/i386-pc-msdosdjgpp/include/' for a cross-compiler from a
  77. typical Unix-like environmant to MS-DOS with DJGPP.
  78.  
  79.    Configuration and compilation of the compiler can then be done using
  80. the scripts `cfgpc' and `mkgpc' which are included in the source
  81. distribution in the subdirectory `p/script'. Please call them with the
  82. `-h' option for instructions.
  83.  
  84. 
  85. File: gpc.info,  Node: Crossbuilding,  Prev: Cross-Compilers,  Up: Installation
  86.  
  87. Crossbuilding a compiler.
  88. =========================
  89.  
  90.    Using a cross-compiler to build GNU Pascal results in a compiler
  91. binary that runs on the cross-target platform. This is called
  92. "crossbuilding". A possible reason why anybody would want to do this,
  93. is when the platform on which you want to run the GNU Pascal compiler
  94. is not self-hosting. An example is mingw32.
  95.  
  96.    To crossbuild GNU Pascal, you have to install a cross-compiler for
  97. your target first, see *Note Cross-Compilers::.
  98.  
  99.    As when building a cross-compiler, configuration and compilation of
  100. the compiler can be done using the scripts `cfgpc' and `mkgpc' which
  101. are included in the source distribution in the subdirectory `p/script'.
  102. Please call them with the `-h' option for instructions.
  103.  
  104. 
  105. File: gpc.info,  Node: Borland Pascal,  Next: Invoking GPC,  Prev: Installation,  Up: Top
  106.  
  107. A QuickStart Guide from Borland Pascal to GNU Pascal.
  108. *****************************************************
  109.  
  110.    This chapter is intended to be a QuickStart guide for programmers
  111. who are familiar with Borland Pascal.
  112.  
  113.    Throughout the manual, we talk of "Borland Pascal" or "BP" for
  114. short, to refer to Borland Pascal version 7 for Dos protected mode.
  115. Other versions of Borland Pascal and Turbo Pascall don't differ too
  116. much, but this one was the very last Dos version Borland has published,
  117. so in most if not all cases, you can safely substitute the version
  118. you're familiar with.
  119.  
  120.    "Borland Pascal" and "Turbo Pascal" are registered trademarks of
  121. Inprise Corp./Borland Inc.
  122.  
  123. * Menu:
  124.  
  125. * BP Compatibility::
  126. * BP Incompatibilities::
  127. * IDE versus command line::
  128. * Comments::
  129. * BP Compatible Compiler Directives::
  130. * Units; GPI files and AutoMake::
  131. * Optimization::
  132. * Debugging::
  133. * Objects::
  134. * Strings in BP and GPC::
  135. * Typed Constants::
  136. * Bit; Byte and Memory Manipulation::
  137. * User-defined Operators in GPC::
  138. * Data Types in BP and GPC::
  139. * BP Procedural Types::
  140. * Files::
  141. * Built-in Constants::
  142. * Built-in Operators in BP and GPC::
  143. * Built-in Procedures and Functions::
  144. * Special Parameters::
  145. * Miscellaneous::
  146. * BP and Extended Pascal::
  147. * Portability hints::
  148.  
  149. 
  150. File: gpc.info,  Node: BP Compatibility,  Next: BP Incompatibilities,  Up: Borland Pascal
  151.  
  152. BP Compatibility
  153. ================
  154.  
  155.    GNU Pascal (GPC) is compatible to version 7 of Borland Pascal (BP)
  156. to a large extent and comes with portable replacements of the BP
  157. standard units.
  158.  
  159.    However, BP is a 16-bit compiler while GPC is a 32/64-bit compiler,
  160. so the size of the `Integer' type, for instance, is 16 bits in BP, but
  161. at least 32 bits in GPC. If a BP program has been designed with
  162. portability in mind from the ground up, it may work with GPC without
  163. any change. Programs which rely on byte order, on the internals or
  164. sizes of data types or which use unportable things like interrupts and
  165. assembler code, will need to be changed. The following section lists
  166. the possible problems with solutions.
  167.  
  168.    The GPC Run Time System (RTS) is fairly complete, and you can use
  169. all libraries written for GNU C from GNU Pascal, so there is much less
  170. need to use unportable constructs than there was in BP. (For example,
  171. BP's Turbo Vision library uses assembler to call a local procedure
  172. through a pointer. With GPC you can do this in Pascal just as with
  173. global procedures.) Please do not throw away the advantage of full
  174. portability by sticking to those workarounds.
  175.  
  176.    We have successfully ported real-world projects (with several 10000s
  177. of lines) from BP to GPC, so this is possible for you, too.
  178.  
  179. 
  180. File: gpc.info,  Node: BP Incompatibilities,  Next: IDE versus command line,  Prev: BP Compatibility,  Up: Borland Pascal
  181.  
  182. BP Incompatibilities
  183. ====================
  184.  
  185.    This sections lists the remaining incompatibilities of GPC to BP,
  186. and the problems you might encounter when porting BP programs from
  187. 16-bit Dos to other platforms, and gives solutions for them.
  188.  
  189.    By "incompatibilites" we mean problems that can arise when trying to
  190. compile a valid BP program with GPC. Of course, there are many features
  191. in GPC that BP doesn't know, but we call them extensions unless they
  192. can break valid BP programs, so they are not mentioned here. The
  193. subsequent sections of the `Borland Pascal' chapter mention a number of
  194. useful extensions that you might want to know about but which will not
  195. break your BP code.
  196.  
  197. * Menu:
  198.  
  199. * String type::
  200. * Qualified identifiers::
  201. * Assembler::
  202. * Move; FillChar::
  203. * Real type::
  204. * Graph unit::
  205. * OOP units::
  206. * Keep; GetIntVec; SetIntVec::
  207. * TFDDs::
  208. * Mem; Port; Ptr; Seg; Ofs; PrefixSeg; etc.::
  209. * Endianness assumptions::
  210.  
  211.    Some of the differences can be "overcome" by command-line switches.
  212. As a summary:
  213.  
  214.      --borland-pascal -w --uses=System -D__BP_TYPE_SIZES__ --pack-struct
  215.      -D__BP_RANDOM__ -D__BP_UNPORTABLE_ROUTINES__
  216.  
  217.    But please read the following notes, and don't use these switches
  218. indiscriminately when not necessary. There are reasons why they are not
  219. GPC's defaults.
  220.  
  221. * Menu:
  222.  
  223. * - -borland-pascal - disable GPC extensions::
  224. * -w - disable all warnings::
  225. * - -uses=System - Swap; HeapError; etc.::
  226. * -D__BP_TYPE_SIZES__ - small integer types etc.::
  227. * - -pack-struct - disable structure alignment::
  228. * -D__BP_RANDOM__ - BP compatible pseudo random number generator::
  229. * -D__BP_UNPORTABLE_ROUTINES__ - Intr; DosVersion; etc.::
  230.  
  231. 
  232. File: gpc.info,  Node: String type,  Next: Qualified identifiers,  Up: BP Incompatibilities
  233.  
  234. String type
  235. -----------
  236.  
  237.    GPC's internal string format (Extended Pascal string schema) is
  238. different from BP's. BP compatible "short strings" will be implemented
  239. in GPC soon, but in the meantime, you'll have to live with the
  240. difference. In general, GPC's format has many advantages (no length
  241. limit of 255 characters, constant and reference parameters always know
  242. about their capacity, etc.), but you will see differences if you:
  243.  
  244.    - declare a variable as `String' without a capacity. However, GPC
  245.      will assume 255 then (like BP) and only warn about it (and not even
  246.      this when using `--borland-pascal', see below), so that's not a
  247.      real problem. The "clean" way, however, is to declare `String
  248.      [255]' when you mean so (but perhaps you'll prefer `String
  249.      (2000)'? :-).
  250.  
  251.    - access "character 0" which happens to hold the length in BP. This
  252.      does not work with string schemata. Use `Length' to get the
  253.      length, and `SetLength' to modify it.
  254.  
  255.    - try to `FillChar' a string, e.g.  `FillChar (StringVar, 256, 0);',
  256.      which would overwrite the `Capacity' field. Using `FillChar
  257.      (StringVar [1], ...);' is alright since it accesses the characters
  258.      of the string, not the `Capacity' and `Length' fields. If you want
  259.      to set the length to zero, use `SetLength' (see above) or simply
  260.      assign an empty string (`StringVar := '''). This is more efficient
  261.      than clearing all the characters, anyway, and has the same effect
  262.      for all normal purposes.
  263.  
  264.    - try to read or write strings from/to _binary_ files (`Text' files
  265.      are no problem). You will have to rewrite the code. If you also
  266.      want to get rid of the 255 character limit and handle endianness
  267.      issues (see below) in one go, you can use the
  268.      `ReadStringLittleEndian' etc. routines (*note Run Time System::),
  269.      but if you need BP compatible strings (i.e., with a one-byte
  270.      length field) in data files, you cannot use them (but you can
  271.      easily modify them for this purpose).
  272.  
  273. 
  274. File: gpc.info,  Node: Qualified identifiers,  Next: Assembler,  Prev: String type,  Up: BP Incompatibilities
  275.  
  276. Qualified identifiers
  277. ---------------------
  278.  
  279.    GPC does not yet support "qualified identifiers". They will be
  280. implemented soon. In the meantime, just don't use them, sorry. (In
  281. general, using the same global identifier in different units can easily
  282. be confusing, so it's not bad practice to avoid this, anyway.)
  283.  
  284. 
  285. File: gpc.info,  Node: Assembler,  Next: Move; FillChar,  Prev: Qualified identifiers,  Up: BP Incompatibilities
  286.  
  287. Assembler
  288. ---------
  289.  
  290.    GPC's inline assembler is not compatible to BP's. It uses "AT&T
  291. syntax", supports a large variety of processors and works together with
  292. GPC's optimizer. So, either convert your inline assembly to AT&T
  293. syntax, or (usually better) to Pascal, or put it into an external file
  294. which you can assemble with your favourite (32 bit) assembler. A
  295. tutorial for using the GPC inline assembler is available at
  296. `ftp://agnes.dida.physik.uni-essen.de/gnu-pascal/contrib/gpcasm.zip'
  297.  
  298.    Since many things you usually do with assembler in BP are provided
  299. by GPC's Run Time System (RTS), you will not need the inline assembler
  300. as often as in BP. (See *Note Portability hints::.)
  301.  
  302.    The same applies to BP's `inline' directive for hand-written machine
  303. code. GPC's `inline' directive works for Pascal routines (*note
  304. Miscellaneous::), so you'll have to convert any hand-written machine
  305. code to Pascal (and thereby make it more readable, portable and easier
  306. to maintain while still getting the performance of inline code).
  307.  
  308. 
  309. File: gpc.info,  Node: Move; FillChar,  Next: Real type,  Prev: Assembler,  Up: BP Incompatibilities
  310.  
  311. Move; FillChar
  312. --------------
  313.  
  314.    GPC supports `Move' and `FillChar', and they're fully BP compatible.
  315. However, some data structures have different internal formats which may
  316. become relevant when using these procedures. E.g., using `Move' on file
  317. variables does not work in GPC (there are reasons why assigning file
  318. variables with `:=' is not allowed in Pascal, and circumventing this
  319. restriction with `Move' is not a good idea). For other examples, see
  320. *Note String type::, *Note Real type::, and *Note Endianness
  321. assumptions::.
  322.  
  323. 
  324. File: gpc.info,  Node: Real type,  Next: Graph unit,  Prev: Move; FillChar,  Up: BP Incompatibilities
  325.  
  326. Real type
  327. ---------
  328.  
  329.    GPC does not support BP's 6-byte `Real' type. It supports `Single',
  330. `Double' and `Extended' which, at least on the x86 and some other
  331. processors, are compatible to BP.
  332.  
  333.    For BP's 6-byte `Real' type, GPC's `System' unit provides an
  334. emulation, called `BPReal', as well as conversion routines to GPC's
  335. `Real' type (which is the same as `Double'), called `RealToBPReal' and
  336. `BPRealToReal'. You'll probably only need them when reading or writing
  337. binary files containing values of the BP 6-byte real type. There are no
  338. operators (e.g., `+') available for `BPReal', but since GPC supports
  339. operator overloading, you could define them yourself (e.g., convert to
  340. `Real', do the operation, and convert back). Needless to say that this
  341. is very inefficient and should not be done for any serious
  342. computations. Better convert your data after reading them from the file
  343. and before writing them back, or simply convert your data files once
  344. (the other types are more efficient even with BP on any non-prehistoric
  345. processor, anyway).
  346.  
  347. 
  348. File: gpc.info,  Node: Graph unit,  Next: OOP units,  Prev: Real type,  Up: BP Incompatibilities
  349.  
  350. Graph unit
  351. ----------
  352.  
  353.    A somewhat BP compatible Graph unit exists but is distributed
  354. separately due to its license. It is known to work under DJGPP and
  355. Linux/x86, under development under Mingw, and should work under any
  356. Unix system with X11 (but only tested on a few systems).
  357.  
  358. 
  359. File: gpc.info,  Node: OOP units,  Next: Keep; GetIntVec; SetIntVec,  Prev: Graph unit,  Up: BP Incompatibilities
  360.  
  361. OOP units
  362. ---------
  363.  
  364.    The OOP stuff (Turbo Vision etc.) is not yet completed, but work on
  365. several projects is underway. If you want information about the current
  366. status or access to development source, please contact the GPC mailing
  367. list.
  368.  
  369. 
  370. File: gpc.info,  Node: Keep; GetIntVec; SetIntVec,  Next: TFDDs,  Prev: OOP units,  Up: BP Incompatibilities
  371.  
  372. Keep; GetIntVec; SetIntVec
  373. --------------------------
  374.  
  375.    The routines `Keep', `GetIntVec' and `SetIntVec' in the `Dos' unit
  376. do not even make sense on DJGPP (32 bit Dos extender). If your program
  377. uses these, it is either a low-level Dos utility for which porting to a
  378. 32 bit environment might cause bigger problems (because the internal
  379. issues of DPMI become relevant which are usually hidden by DJGPP), or
  380. it installs interrupt handlers which will have to be thought about more
  381. carefully because of things like locking memory, knowing about and
  382. handling the differences between real and protected mode interrupts,
  383. etc. For these kinds of things, we refer you to the DJGPP FAQ (*note
  384. DJGPP FAQ: (djgppfaq)Top.).
  385.  
  386. 
  387. File: gpc.info,  Node: TFDDs,  Next: Mem; Port; Ptr; Seg; Ofs; PrefixSeg; etc.,  Prev: Keep; GetIntVec; SetIntVec,  Up: BP Incompatibilities
  388.  
  389. TFDDs
  390. -----
  391.  
  392.    The internal structure of file variables (`FileRec' and `TextRec' in
  393. BP's `Dos' unit and `TFileRec' and `TTextRec' in BP's `WinDos' unit) is
  394. different in GPC.  However, as far as "Text File Device Drivers"
  395. (TFDDs) are concerned, GPC offers a more powerful mechanism. Please see
  396. the RTS reference (*note Run Time System::), under `AssignTFDD'.
  397.  
  398. 
  399. File: gpc.info,  Node: Mem; Port; Ptr; Seg; Ofs; PrefixSeg; etc.,  Next: Endianness assumptions,  Prev: TFDDs,  Up: BP Incompatibilities
  400.  
  401. Mem; Port; Ptr; Seg; Ofs; PrefixSeg; etc.
  402. -----------------------------------------
  403.  
  404.    Those few routines in the `System' unit that deal with segmented
  405. pointers (e.g., `Ptr') are emulated in such a way that such ugly BP
  406. constructs like
  407.      PInteger (Ptr (Seg (a), Ofs (a) + 6 * SizeOf (Integer)))^ = 42
  408.    work in GPC, but they do not provide access to absolute memory
  409. addresses. Neither do `absolute' variables (which take a simple address
  410. in the program's address space in GPC, rather than a segmented
  411. address), and the `Mem' and `Port' arrays don't exist in GPC.
  412.  
  413.    As a replacement for `Port' on x86 processors, you can use the
  414. routines provided in the `Ports' unit, *Note Ports::. If you want to
  415. access absolute memory addresses in the first megabyte under DJGPP, you
  416. can't do this with normal pointers because DJGPP programs run in a
  417. protected memory environment, unless you use a dirty trick called "near
  418. pointer hack". Please see the DJGPP FAQ (*note DJGPP FAQ:
  419. (djgppfaq)Top.) for this and for other ways.
  420.  
  421.    For similar reasons, the variable `PrefixSeg' in the `System' unit
  422. is not supported. Apart from "TSR"s, its only meaningful use in BP
  423. might be the setting of environment variables.  GPC provides the
  424. `SetEnv' and `UnSetEnv' procedures for this purpose which you can use
  425. instead of any BP equivalents based on `PrefixSeg'. (However note that
  426. they will modify the program's own and its childs' environment, not its
  427. parent's environment. This is a property - most people call it a
  428. feature - of the environments, including DJGPP, that GPC compiled
  429. programs run in.)
  430.  
  431. 
  432. File: gpc.info,  Node: Endianness assumptions,  Next: - -borland-pascal - disable GPC extensions,  Prev: Mem; Port; Ptr; Seg; Ofs; PrefixSeg; etc.,  Up: BP Incompatibilities
  433.  
  434. Endianness assumptions
  435. ----------------------
  436.  
  437.    GPC also runs on big-endian systems (*note Endianness::). This is,
  438. of course, a feature of GPC, but might affect your programs when
  439. running on a big-endian system if they make assumptions about
  440. endianness, e.g., by using type casts (or `absolute' declarations or
  441. variant records misused as type casts) in certain ways. Please see the
  442. demo program `absdemo.pas' for an example and how to solve it.
  443.  
  444.    Endianness is also relevant (the more common case) when exchanging
  445. data between different machines, e.g. via binary files or over a
  446. network. Since the latter is not easily possible in BP, and the
  447. techniques to solve the problems are mostly the same as for files, we
  448. concentrate on files here.
  449.  
  450.    First, you have to choose the endianness to use for the file. Most
  451. known data formats have a specified endianness (usually that of the
  452. processor on which the format was originally created). If you define
  453. your own binary data format, you're free to choose the endianness to
  454. use.
  455.  
  456.    Then, when reading or writing values larger than one byte from/to
  457. the file, you have to convert them. GPC's Run Time System supports this
  458. by some routines. E.g., you can read an array from a little-endian file
  459. with the procedure `BlockReadLittleEndian', or write one to a
  460. big-endian file with `BlockWriteBigEndian'.  _Note:_ The endianness in
  461. the procedure names refers to the file, not the system - the routines
  462. know about the endianness of the system they run on, but you have to
  463. tell them the endianness of the file to use. This means you do not have
  464. to (and must not) use an `ifdef' to use the version matching the
  465. system's endianness.
  466.  
  467.    When reading or writing records or other more complicated
  468. structures, either read/write them field by field using
  469. `BlockReadBigEndian' etc., or read/write them with the regular
  470. `BlockRead' and `BlockWrite' procedures and convert each field after
  471. reading or before writing using procedures like `ConvertFromBigEndian'
  472. or `ConvertToLittleEndian' (but remember, when writing, to undo the
  473. conversion afterwards, if you want to keep using the data - this is not
  474. necessary with `BlockWriteLittleEndian' etc.).
  475.  
  476.    Especially for strings, there are ready-made procedures like
  477. `ReadStringBigEndian' or `WriteStringLittleEndian' which will
  478. read/write the length as a 64 bit value (much space for really long
  479. strings :-) in the given endianness, followed by the characters (which
  480. have no endianness problem).
  481.  
  482.    All these routines are described in detail in the RTS reference
  483. (*note Run Time System::), under `endianness'. The demo program
  484. `endiandemo.pas' contains an example on how to use these routines.
  485.  
  486. 
  487. File: gpc.info,  Node: - -borland-pascal - disable GPC extensions,  Next: -w - disable all warnings,  Prev: Endianness assumptions,  Up: BP Incompatibilities
  488.  
  489. - -borland-pascal - disable GPC extensions
  490. ------------------------------------------
  491.  
  492.    GPC warns about some BP constructs which are especially "dirty",
  493. like misusing typed constants as initialized variables. GPC also
  494. supports some features that may conflict with BP code, like macros.
  495. The command line option `--borland-pascal' disables both, so you might
  496. want to use it for a first attempt to compile your BP code under GPC.
  497. However, we suggest you try compiling without this switch and fixing
  498. any resulting problems as soon as you've become acquainted with GPC.
  499.  
  500. 
  501. File: gpc.info,  Node: -w - disable all warnings,  Next: - -uses=System - Swap; HeapError; etc.,  Prev: - -borland-pascal - disable GPC extensions,  Up: BP Incompatibilities
  502.  
  503. -w - disable all warnings
  504. -------------------------
  505.  
  506.    Even in `--borland-pascal' mode, GPC may warn about some dangerous
  507. things. To disable *all* warnings, you can use the `-w' option (note:
  508. lower-case `w'!). This is not recommended at all, but you may consider
  509. it more BP compatible...
  510.  
  511. 
  512. File: gpc.info,  Node: - -uses=System - Swap; HeapError; etc.,  Next: -D__BP_TYPE_SIZES__ - small integer types etc.,  Prev: -w - disable all warnings,  Up: BP Incompatibilities
  513.  
  514. - -uses=System - Swap; HeapError; etc.
  515. --------------------------------------
  516.  
  517.    A few exotic BP routines and declarations (e.g., `Swap' and
  518. `HeapError') are contained in a `System' unit, *Note System::, which
  519. GPC (unlike BP) does not automatically use in each program. To use it,
  520. you can add a `uses System;' clause to your program. If you don't want
  521. to change your code, the command line option `--uses=System' will do
  522. the same.
  523.  
  524. 
  525. File: gpc.info,  Node: -D__BP_TYPE_SIZES__ - small integer types etc.,  Next: - -pack-struct - disable structure alignment,  Prev: - -uses=System - Swap; HeapError; etc.,  Up: BP Incompatibilities
  526.  
  527. -D__BP_TYPE_SIZES__ - small integer types etc.
  528. ----------------------------------------------
  529.  
  530.    Since GPC runs on 32 and 64 bit platforms, integer types have larger
  531. sizes than in BP. However, if you use the `System' unit (*note -
  532. -uses=System - Swap; HeapError; etc.::) and define the symbol
  533. `__BP_TYPE_SIZES__' (by giving `-D__BP_TYPE_SIZES__' on the command
  534. line), it will redeclare the types to the sizes used by BP. This is
  535. less efficient and more limiting, but might be necessary if your
  536. program relies on the exact type sizes.
  537.  
  538. 
  539. File: gpc.info,  Node: - -pack-struct - disable structure alignment,  Next: -D__BP_RANDOM__ - BP compatible pseudo random number generator,  Prev: -D__BP_TYPE_SIZES__ - small integer types etc.,  Up: BP Incompatibilities
  540.  
  541. - -pack-struct - disable structure alignment
  542. --------------------------------------------
  543.  
  544.    GPC by default aligns fields of records and arrays suitably for
  545. higher performance, while BP doesn't. If you don't want the alignment
  546. (e.g., because the program relies on the internal format of your
  547. structures), either declare the relevant structures as `packed' (which
  548. BP also accepts, but ignores), or give the `--pack-struct' option.
  549.  
  550. 
  551. File: gpc.info,  Node: -D__BP_RANDOM__ - BP compatible pseudo random number generator,  Next: -D__BP_UNPORTABLE_ROUTINES__ - Intr; DosVersion; etc.,  Prev: - -pack-struct - disable structure alignment,  Up: BP Incompatibilities
  552.  
  553. -D__BP_RANDOM__ - BP compatible pseudo random number generator
  554. --------------------------------------------------------------
  555.  
  556.    GPC uses a more elaborate pseudo random number generator than BP
  557. does. Using the `Random' and `Randomize' functions works the same way,
  558. but there is no `RandSeed' variable (but a `SeedRandom' procedure).
  559. However, if you use the `System' unit (*note - -uses=System - Swap;
  560. HeapError; etc.::) and define the symbol `__BP_RANDOM__' (by giving
  561. `-D__BP_RANDOM__' on the command line), it will provide a 100% BP
  562. compatible pseudo random number generator, including the `RandSeed'
  563. variable, which will produce exactly the same sequence of pseudo random
  564. numbers that BP's pseudo random number generator does. Even the
  565. `Randomize' function will then behave exactly like in BP.
  566.  
  567. 
  568. File: gpc.info,  Node: -D__BP_UNPORTABLE_ROUTINES__ - Intr; DosVersion; etc.,  Prev: -D__BP_RANDOM__ - BP compatible pseudo random number generator,  Up: BP Incompatibilities
  569.  
  570. -D__BP_UNPORTABLE_ROUTINES__ - Intr; DosVersion; etc.
  571. -----------------------------------------------------
  572.  
  573.    A few more routines in the `Dos' and `WinDos' units besides the ones
  574. mentioned under *Note Keep; GetIntVec; SetIntVec::, like `Intr' or
  575. `DosVersion', are meaningless on non-Dos systems. By default, the `Dos'
  576. unit does not provide these routines (it only provides those that are
  577. meaningful on all systems, which are most of its routines, including
  578. the most commonly used ones). If you need the unportable ones, you get
  579. them by defining the symbol `__BP_UNPORTABLE_ROUTINES__' (by giving
  580. `-D__BP_UNPORTABLE_ROUTINES__' on the command line). If you use `Intr'
  581. or `MsDos', your program will only compile under DJGPP then. Other
  582. routines, e.g. `DosVersion' are emulated quite roughly on other
  583. systems. Please see the notes in the `Dos' unit (*note Dos::) for
  584. details.
  585.  
  586. 
  587. File: gpc.info,  Node: IDE versus command line,  Next: Comments,  Prev: BP Incompatibilities,  Up: Borland Pascal
  588.  
  589. IDE versus command line
  590. =======================
  591.  
  592.    On the Dos (DJGPP) and Linux platforms, you can use RHIDE for GNU
  593. Pascal; check the subdirectories of your DJGPP distribution.
  594.  
  595.    Unfortunately, there is no IDE which would run on all platforms. We
  596. are working on it, but this will take some time. Please be patient - or
  597. offer your help!
  598.  
  599.    Without an IDE, the GNU Pascal Compiler, GPC, is called about like
  600. the command-line version of the Borland Pascal Compiler, BPC. Edit your
  601. source file(s) with your favorite ASCII editor, then call GNU Pascal
  602. with a command line like
  603.  
  604.      C:\GNU-PAS> gpc hello.pas -o hello.exe
  605.  
  606. on your Dos or OS/2 box or
  607.  
  608.      myhost:/home/joe/gnu-pascal> gpc hello.pas -o hello
  609.  
  610. on your Unix (or Unix-compatible) system.
  611.  
  612.    Don't omit the `.pas' suffix: GPC is a common interface for a Pascal
  613. compiler, a C, ObjC and C++ compiler, an assembler, a linker, and
  614. perhaps an Ada and a FORTRAN compiler. From the extension of your
  615. source file GPC figures out which compiler to run. GPC recognizes
  616. Pascal sources by the extension `.pas', `.p', `.pp' or `.dpr'.
  617.  
  618.    The `-o' is a command line option which tells GPC how the executable
  619. has to be named. If not given, the executable will be called `a.out'
  620. (Unix) or `a.exe' (Dos). However, you can use the
  621. `--executable-file-name' to tell GPC to always call the executable like
  622. the source (with the extension removed under Unix and changed to `.exe'
  623. under Dos).
  624.  
  625.    Note that GPC is case-sensitive concerning file names and options,
  626. so it will _not_ work if you type
  627.  
  628.      C:\GNU-PAS> GPC HELLO.PAS -O HELLO.EXE
  629.  
  630.    GPC is a very quiet compiler and doesn't print anything on the
  631. screen unless you request it or there is an error. If you want to see
  632. what is going on, invoke GPC with additional options:
  633.  
  634.      -Q            "don't be quiet"  (or: Quassel-Modus in German)
  635.  
  636. (with _capital_ `Q'!) means that GPC prints out the names of procedures
  637. and functions it processes, and
  638.  
  639.      --verbose
  640.  
  641. or abbreviated
  642.  
  643.      -v
  644.  
  645. means that GPC informs you about the stages of compilation, i.e.
  646. preprocessing, compiling, assembling, and linking.
  647.  
  648.    One example (this time for OS/2):
  649.  
  650.      [C:\GNU-Pascal] gpc --verbose -Q hello.pas
  651.  
  652.    Throughout this chapter, we will tell you about a lot of
  653. command-line switches. They are all invoked this way.
  654.  
  655.    After compilation, there will be an executable `hello' file in the
  656. current directory. (`hello.exe' on Dos or OS/2.) Just run it and enjoy.
  657. If you're new to Unix, please note that the current directory is not on
  658. the PATH in most installations, so you might have to run your program
  659. as `./hello'. This also helps to avoid name conflicts with other
  660. programs. Such conflicts are especially common with the program name
  661. `test' which happens to be a standard utility under Unix that does not
  662. print any output. If you call your program `test.pas', compile it, and
  663. then invoke `test', you will usually not run your program, but the
  664. utility which leads to mysterious problems. So, invoke your program as
  665. `./test' or, better yet, avoid the name `test' for your programs.
  666.  
  667.    If there are compilation errors, GNU Pascal will not stop
  668. compilation after the first one - as Borland Pascal does - but try to
  669. catch all errors in one compilation. If you get more error messages
  670. than your screen can hold, you can catch them in a file (e.g.
  671. `gpc.out') or pipe them to a program like `more' in the following way:
  672.  
  673.      gpc hello.pas 2> gpc.out
  674.  
  675.    This works with OS/2 and any bash-like shell under Unix; for Dos you
  676. must get a replacement for `command.com' which supports this kind of
  677. redirection, or use the `redir' utility (see also the DJGPP FAQ, *Note
  678. DJGPP FAQ: (djgppfaq)Top.):
  679.  
  680.      C:\GNU-PAS> redir -eo gpc hello.pas -o hello.exe | more
  681.  
  682.    You can also use Borland's IDE for GNU Pascal on the Dos platform:
  683. Install the GNU Pascal Compiler in the Tools menu (via Options/Tools).
  684.  
  685.      Name:       GNU Pascal
  686.      Path:       gpc
  687.      Arguments:  $SAVE ALL --executable-file-name $NAME($EDNAME).pas
  688.      HotKey:     Shift+F9
  689.  
  690.    Note once more that GPC is case-sensitive, so it is important to
  691. specify `.pas' instead of the `.PAS' Borland Pascal would append
  692. otherwise!
  693.  
  694.    You can include more command-line arguments to GNU Pascal (e.g.
  695. `--automake'; see below) as you will learn more about them.
  696.  
  697.    Since Borland Pascal will try to recompile your program if you use
  698. its `Run' menu function, you will need another tool to run your program:
  699.  
  700.      Name:       Run Program
  701.      Path:       command.com
  702.      Arguments:  /c $NAME($EDNAME)
  703.      HotKey:     Shift+F10
  704.  
  705. 
  706. File: gpc.info,  Node: Comments,  Next: BP Compatible Compiler Directives,  Prev: IDE versus command line,  Up: Borland Pascal
  707.  
  708. Comments
  709. ========
  710.  
  711.    GPC supports comments surrounded by `{ }' and `(* *)', just like BP
  712. does. According to the ISO 7185 and ISO 10206 standards, Pascal allows
  713. comments opened with `(*' and closed with `}'. Borland Pascal does not
  714. support such "mixed" comments, so you might have sources where passages
  715. containing comments are "commented out" using the other kind of comment
  716. delimiters. GPC's default behaviour is (like BP) not to allow mixed
  717. comments, so you don't need to worry about this. However, if you happen
  718. to like mixed comments, you can turn them on either by a command-line
  719. option, or by a compiler directive:
  720.  
  721.      --mixed-comments     {$mixed-comments}    (*$mixed-comments*)
  722.  
  723.    GPC supports nested comments (e.g., `{ foo { bar } baz }'), but they
  724. are disabled by default (compatible to BP which doesn't know nested
  725. comments at all). You can enable them with the option
  726. `--nested-comments' (or the equivalent compiler directive)
  727.  
  728.    GPC also supports Delphi style comments starting with `//' and
  729. extending until the end of the line. This comment style is activated by
  730. default unless one of the `--standard-pascal', `--extended-pascal',
  731. `--object-pascal' or `--borland-pascal' dialect options is given. You
  732. can turn them on or off with the `--[no-]delphi-comments' option.
  733.  
  734. 
  735. File: gpc.info,  Node: BP Compatible Compiler Directives,  Next: Units; GPI files and AutoMake,  Prev: Comments,  Up: Borland Pascal
  736.  
  737. BP Compatible Compiler Directives
  738. =================================
  739.  
  740.    All of BP's one-letter compiler directives are supported by GPC,
  741. though some of them are ignored because they are not necessary under
  742. GPC. Besides, GPC supports a lot more directives. For an overview, see
  743. *Note Compiler Directives::.
  744.  
  745. 
  746. File: gpc.info,  Node: Units; GPI files and AutoMake,  Next: Optimization,  Prev: BP Compatible Compiler Directives,  Up: Borland Pascal
  747.  
  748. Units, GPI files and AutoMake
  749. =============================
  750.  
  751.    You can use units in the same way as in Borland Pascal. However,
  752. there are some additional features.
  753.  
  754.    Concerning the syntax of a unit, you can, if you want, use Extended
  755. Pascal syntax to specify a unit initializer, i.e., instead of writing
  756.  
  757.      begin
  758.        ...
  759.      end.
  760.  
  761. at the end of the unit, you can get the same result with
  762.  
  763.      to begin do
  764.        begin
  765.          ...
  766.        end;
  767.  
  768. and there also exists
  769.  
  770.      to end do
  771.        begin
  772.          ...
  773.        end;
  774.  
  775. which specifies a finalization routine. You can use this instead of
  776. Borland Pascal's exit procedures, but for compatibility, the included
  777. `System' unit also provides the `ExitProc' variable. The `to begin do'
  778. and/or `to end do' parts must be followed by the final `end.'. See
  779. *Note Modules::, for information about Extended Pascal modules, an
  780. alternative to units.
  781.  
  782.    When GPC compiles a unit, it produces two files: an `.o' object file
  783. (compatible with other GNU compilers such as GNU C) plus a `.gpi' file
  784. which describes the interface.
  785.  
  786.    If you are interested in the internal format of GPI file, see *Note
  787. GPI files::.
  788.  
  789.    If you want to compile a program that uses units, you must "make"
  790. the project. (This is the command-line switch `-M' or the IDE keystroke
  791. `F9' in BP.) For this purpose, GPC provides the command-line switch
  792. `--automake':
  793.  
  794.      gpc --automake hello.pas
  795.  
  796.    If you want to force everything to be rebuilt rather than only
  797. recompile changed files (`-B' or "build" in BP), use `--autobuild'
  798. instead of `--automake':
  799.  
  800.      gpc --autobuild hello.pas
  801.  
  802.    For more information about the AutoMake mechanism, see *Note
  803. AutoMake::.
  804.  
  805.    If you do not want to use the AutoMake mechanism for whatever
  806. reason, you can also compile every unit manually and then link
  807. everything together.
  808.  
  809.    GPC does not automatically recognize that something is a unit and
  810. cannot be linked; you have to tell this by a command line switch:
  811.  
  812.      -c            only compile, don't link.
  813.  
  814.    (If you omit this switch when compiling a unit, you only get a
  815. linker error message `undefined reference to `main''.  Nothing serious.)
  816.  
  817.    For example, to compile two units, use:
  818.  
  819.      gpc -c myunit1.pas myunit2.pas
  820.  
  821.    When you have compiled all units, you can compile a program that
  822. uses them without using `--automake':
  823.  
  824.      gpc hello.pas
  825.  
  826.    However, using `--automake' is recommended, since it will recompile
  827. units that were modified.
  828.  
  829.    You could also specify the program and the units in one command line:
  830.  
  831.      gpc hello.pas myunit1.pas myunit2.pas
  832.  
  833.    One of the purposes of writing units is to compile them separately.
  834. However, GNU Pascal allows you to have one or more units in the same
  835. source file (producing only one `.o' file but separate `.gpi' files).
  836. You even can have a program and one or more units in one source file;
  837. in this case, no `.o' file is produced at all.
  838.  
  839. 
  840. File: gpc.info,  Node: Optimization,  Next: Debugging,  Prev: Units; GPI files and AutoMake,  Up: Borland Pascal
  841.  
  842. Optimization
  843. ============
  844.  
  845.    GNU Pascal is a 32/64 bit compiler with excellent optimization
  846. algorithms (which are identically the same as those of GNU C). There
  847. are six optimization levels, specified by the command line options
  848. `-O', `-O2', ..., `-O6'.
  849.  
  850.    One example:
  851.  
  852.      program OptimizationDemo;
  853.      
  854.      procedure Foo;
  855.      var
  856.        A, B : Integer;
  857.      begin
  858.        A := 3;
  859.        B := 4;
  860.        WriteLn (A + B)
  861.      end;
  862.      
  863.      begin
  864.        Foo
  865.      end.
  866.  
  867.    When GNU Pascal compiles this program with optimization (`-O3'), it
  868. recognizes that the argument to `WriteLn' is the constant 7 - and
  869. optimizes away the variables `A' and `B'. If the variables were global,
  870. they would not be optimized away because they might be accessed from
  871. other places, but the constant 7 would still be optimized.
  872.  
  873.    For more about optimization, see the GNU C documentation.
  874.  
  875. 
  876. File: gpc.info,  Node: Debugging,  Next: Objects,  Prev: Optimization,  Up: Borland Pascal
  877.  
  878. Debugging
  879. =========
  880.  
  881.    The command line option `-g' specifies generation of debugging
  882. information for GDB, the GNU debugger. GDB comes with its own
  883. documentation. Currently, GDB does not understand Pascal syntax, so you
  884. should be familiar with C expressions if you want to use it.
  885.  
  886.    See also "Notes for debugging" in the "Programming" chapter; see
  887. *Note Notes for Debugging::.
  888.  
  889.    Sometimes it is nice to have a look at the assembler output of the
  890. compiler. You can do this in a debugger or disassembler (which is the
  891. only way to do it in BP), but you can also tell GPC to produce
  892. assembler code directly: When you specify the `-S' command line option,
  893. GPC produces an `.s' file instead of an `.o' file.  The `.s' file
  894. contains assembler source for your program. More about this in the next
  895. section.
  896.  
  897. 
  898. File: gpc.info,  Node: Objects,  Next: Strings in BP and GPC,  Prev: Debugging,  Up: Borland Pascal
  899.  
  900. Objects
  901. =======
  902.  
  903.    Objects in the Borland Pascal 7.0 notation are implemented into GNU
  904. Pascal with the following differences:
  905.  
  906.    * the `private', `protected', `public' and `published' directives
  907.      are recognized but ignored,
  908.  
  909.    * data fields and methods may be mixed:
  910.           type
  911.             MyObj = object
  912.               x: Integer;
  913.               procedure Foo; virtual;
  914.               y: Real;
  915.               function Bar: Char;
  916.             end;
  917.  
  918. 
  919. File: gpc.info,  Node: Strings in BP and GPC,  Next: Typed Constants,  Prev: Objects,  Up: Borland Pascal
  920.  
  921. Strings in BP and GPC
  922. =====================
  923.  
  924.    Strings are "Schema types" in GNU Pascal which is something more
  925. advanced than Borland-style strings. For variables, you cannot specify
  926. just `String' as a type like in Borland Pascal; for parameters and
  927. pointer types you can. There is no 255 characters length limit.
  928. According to Extended Pascal, the maximum string length must be in
  929. (parentheses); GNU Pascal accepts [brackets], too, however, like BP.
  930.  
  931.    For more about strings and schema types see *Note Schema Types::.
  932.  
  933.    GPC supports Borland Pascal's string handling functions and some
  934. more (see *Note String Operations::):
  935.  
  936. Borland Pascal           GNU Pascal
  937. Length                   Length
  938. Pos                      Pos, Index (1)
  939. Str                      Str, WriteStr (1) (2)
  940. Val                      Val, ReadStr (2)
  941. Copy                     Copy, SubStr, MyStr [2 .. 7] (3)
  942. Insert                   Insert
  943. Delete                   Delete
  944. MyStr [0] := #7          SetLength (MyStr, 7)
  945. =, <>, <, <=, >, >=      =, <>, <, <=, >, >= (4)
  946.                          EQ, NE, LT, LE, GT, GE
  947. n/a                      Trim
  948.  
  949.    Notes:
  950.  
  951.    (1) The order of parameters of the Extended Pascal routines
  952. (`Index', `WriteStr') is different from the Borland Pascal routines.
  953.  
  954.    (2) `ReadStr' and `WriteStr' allow an arbitrary number of arguments,
  955. and the arguments are not limited to numbers.  `WriteStr' also allows
  956. comfortable formatting like `WriteLn' does, e.g.  `WriteStr (Dest, Foo
  957. : 20, Bar, 1/3 : 10 : 2)'.
  958.  
  959.    (3) `SubStr' reports a runtime error if the requested substring does
  960. not fit in the given string, `Copy' does not (like in BP).
  961.  
  962.    (4) By default, the string operators behave like in BP. However, if
  963. you use the option `--no-exact-compare-strings' or `--extended-pascal',
  964. they ignore differences of trailing blanks, so, e.g., `'foo'' and `'foo
  965.  '' are considered equal. The corresponding functions (`EQ', ...)
  966. always do exact comparisons.
  967.  
  968. 
  969. File: gpc.info,  Node: Typed Constants,  Next: Bit; Byte and Memory Manipulation,  Prev: Strings in BP and GPC,  Up: Borland Pascal
  970.  
  971. Typed Constants
  972. ===============
  973.  
  974.    GNU Pascal supports Borland Pascal's "typed constants" but also
  975. Extended Pascal's initialized variables:
  976.  
  977.      var
  978.        x : Integer value 7;
  979.  
  980.    or
  981.  
  982.      var
  983.        x : Integer = 7;
  984.  
  985.    When a typed constant is misused as an initialized variable, a
  986. warning is given unless you specify `--borland-pascal'.
  987.  
  988.    When you want a local variable to preserve its value, define it as
  989. `static' instead of using a typed constant. Typed constants also become
  990. static automatically for Borland Pascal compatibility, but it's better
  991. not to rely on this "feature" in new programs.  Initialized variables
  992. do not become static automatically.
  993.  
  994.      program StaticDemo;
  995.      
  996.      procedure Foo;
  997.      { x keeps its value between two calls to this procedure }
  998.      var
  999.        x : static Integer = 0;
  1000.      begin
  1001.        WriteLn (x);
  1002.        Inc (x)
  1003.      end;
  1004.      
  1005.      begin
  1006.        Foo;
  1007.        Foo;
  1008.        Foo;
  1009.      end.
  1010.  
  1011.    For records and arrays, GPC supports both BP style and Extended
  1012. Pascal style initializers. When you initialize a record, you may omit
  1013. the field names. When you initialize an array, you may provide indices
  1014. with a `:'. However, this additional information is ignored completely,
  1015. so perhaps it's best for the moment to only provide the values ...
  1016.  
  1017.      program BPInitVarDemo;
  1018.      (*$W no-field-name-problem*) (* avoid a warning by GPC *)
  1019.      const
  1020.        A : Integer = 7;
  1021.        B : array [1 .. 3] of Char = ('F', 'o', 'o');
  1022.        C : array [1 .. 3] of Char = 'Bar';
  1023.        Foo : record
  1024.          x, y : Integer;
  1025.        end = (x : 3; y : 4);
  1026.      begin
  1027.      end.
  1028.  
  1029. 
  1030. File: gpc.info,  Node: Bit; Byte and Memory Manipulation,  Next: User-defined Operators in GPC,  Prev: Typed Constants,  Up: Borland Pascal
  1031.  
  1032. Bit, Byte and Memory Manipulation
  1033. =================================
  1034.  
  1035.    The bitwise operators `shl', `shr', `and', `or', `xor' and `not'
  1036. work in GNU Pascal like in Borland Pascal.  As an extension, you can
  1037. use them as procedures, for example
  1038.  
  1039.      program AndProcedureDemo;
  1040.      var x : Integer;
  1041.      begin
  1042.        and (x, $0000ffff);
  1043.      end.
  1044.  
  1045.    as an alternative to
  1046.  
  1047.      program AndOperatorDemo;
  1048.      var x : Integer;
  1049.      begin
  1050.        x := x and $0000ffff;
  1051.      end.
  1052.  
  1053.    GPC accepts the BP style notation `$abcd' for hexadecimal numbers,
  1054. but you also can use Extended Pascal notation:
  1055.  
  1056.      program EPBaseDemo;
  1057.      const
  1058.        Binary =  2#11111111;
  1059.        Octal  =  8#177;
  1060.        Hex    = 16#ff;
  1061.      begin
  1062.      end.
  1063.  
  1064.    and so on up to a basis of 36. Of course, you can mix the notations
  1065. as you like, e.g.:
  1066.  
  1067.      program BPEPBaseDemo;
  1068.      begin
  1069.        WriteLn ($cafe = 2#1100101011111110)
  1070.      end.
  1071.  
  1072.    `Inc' and `Dec' are implemented like in Borland Pascal.  `Pred' and
  1073. `Succ' are generalized according to Extended Pascal and can have a
  1074. second (optional) parameter:
  1075.  
  1076.      procedure SuccDemo;
  1077.      var a : Integer = 42;
  1078.      begin
  1079.        a := Succ (a, 5);
  1080.        WriteLn (a) { 47 }
  1081.      end.
  1082.  
  1083.    BP style `absolute' variables work in the context of overloading
  1084. other variables as well as in the context of specifying an absolute
  1085. address, but the latter is highly unportable and not very useful even
  1086. in Dos protected mode.
  1087.  
  1088.      program BPAbsoluteDemo;
  1089.      
  1090.      type
  1091.        TString = String (80);
  1092.        TTypeChoice = (t_Integer, t_Char, t_String);
  1093.      
  1094.      (* WARNING: BAD STYLE! *)
  1095.      procedure ReadVar (var x : Void; TypeChoice : TTypeChoice);
  1096.      var
  1097.        xInt : Integer absolute x;
  1098.        xChar : Char absolute x;
  1099.        xStr : TString absolute x;
  1100.      begin
  1101.        case TypeChoice of
  1102.          t_Integer : ReadLn (xInt);
  1103.          t_Char    : ReadLn (xChar);
  1104.          t_String  : ReadLn (xStr);
  1105.        end
  1106.      end;
  1107.      
  1108.      var
  1109.        i : Integer;
  1110.        c : Char;
  1111.        s : TString;
  1112.      
  1113.      begin
  1114.        ReadVar (i, t_Integer);
  1115.        ReadVar (c, t_Char);
  1116.        ReadVar (s, t_String);
  1117.        WriteLn (i, ' ', c, ' ', s)
  1118.      end.
  1119.  
  1120.    GNU Pascal knows Borland Pascal's procedures `FillChar' and `Move'.
  1121. However, their use can be dangerous because it often makes implicit
  1122. unportable assumptions about type sizes, endianness, internal
  1123. structures or similar things. Therefore, avoid them whenever possible.
  1124. E.g., if you want to clear an array of strings, don't `FillChar' the
  1125. whole array with zeros (this would overwrite the Schema discriminants,
  1126. see *Note Strings::), but rather use a `for' loop to assign the empty
  1127. string to each string. In fact, this is also more efficient than
  1128. `FillChar', since it only has to set the length field of each string to
  1129. zero.
  1130.  
  1131. 
  1132. File: gpc.info,  Node: User-defined Operators in GPC,  Next: Data Types in BP and GPC,  Prev: Bit; Byte and Memory Manipulation,  Up: Borland Pascal
  1133.  
  1134. User-defined Operators in GPC
  1135. =============================
  1136.  
  1137.    GNU Pascal allows the user to define operators according to the
  1138. Pascal-SC syntax:
  1139.  
  1140.      program PXSCOperatorDemo;
  1141.      
  1142.      type
  1143.        Point = record
  1144.          x, y : Real;
  1145.        end;
  1146.      
  1147.      operator + (a, b : Point) c : Point;
  1148.      begin
  1149.        c.x := a.x + b.x;
  1150.        c.y := a.y + b.y;
  1151.      end;
  1152.      
  1153.      var
  1154.        a, b, c : Point = (42, 0.5);
  1155.      
  1156.      begin
  1157.        c := a + b
  1158.      end.
  1159.  
  1160.    The Pascal-SC operators `+>', `+<', etc. for exact numerical
  1161. calculations are not implemented, but you can define them.
  1162.  
  1163. 
  1164. File: gpc.info,  Node: Data Types in BP and GPC,  Next: BP Procedural Types,  Prev: User-defined Operators in GPC,  Up: Borland Pascal
  1165.  
  1166. Data Types in BP and GPC
  1167. ========================
  1168.  
  1169.    * Integer types have different sizes in Borland and GNU Pascal:
  1170.  
  1171.      Borland Pascal      GNU Pascal         Bits (1)     Signed
  1172.      ShortInt            ByteInt             8           yes
  1173.      Integer             ShortInt           16           yes
  1174.      LongInt             Integer            32           yes
  1175.      Comp                LongInt, Comp      64           yes
  1176.      Byte                Byte                8           no
  1177.      Word                ShortWord          16           no
  1178.      n/a                 Word               32           no
  1179.      n/a                 LongWord           64           no
  1180.  
  1181.      (1) The size of the GNU Pascal types may depend on the platform.
  1182.      The sizes above apply to 32 bit platforms, including the x86.
  1183.  
  1184.      If you care for types with exactly the same size as in Borland
  1185.      Pascal, take a look at the `System' unit and read its comments.
  1186.  
  1187.      You can get the size of a type with `SizeOf' in bytes (like in
  1188.      Borland Pascal) and with `BitSizeOf' in bits, and you can declare
  1189.      types with a specific size (given in bits), e.g.:
  1190.  
  1191.           program IntegerSizeDemo;
  1192.           type
  1193.             MyInt  = Integer (42);  { 42 bits, signed }
  1194.             MyWord = Word (2);      { 2 bits, unsigned, i.e., 0 .. 3 }
  1195.             MyCard = Cardinal (2);  { the same }
  1196.           
  1197.             HalfInt = Integer (BitSizeOf (Integer) div 2);
  1198.               { A signed integer type which is half as big as the normal
  1199.                 `Integer' type, regardless of how big `Integer' is
  1200.                 on any platform the program is compiled on. }
  1201.           
  1202.           begin
  1203.           end.
  1204.  
  1205.    * Borland's real (floating point) types are supported except for the
  1206.      6-byte software Real type (but the `System' unit provides
  1207.      conversion routines for it). GNU Pascals's `Real' type has 8 bytes
  1208.      on the x86 and is the same as `Double'. In addition there are
  1209.      alternative names for real types:
  1210.  
  1211.      Borland Pascal      GNU Pascal
  1212.      Single              Single, ShortReal
  1213.      Real                n/a (1)
  1214.      Double              Double, Real
  1215.      Extended            Extended, LongReal
  1216.      Comp                LongInt, Comp (see above)
  1217.  
  1218.      (1) But see `BPReal', `RealToBPReal' and `BPRealToReal' in GPC's
  1219.      `System' unit.
  1220.  
  1221.    * Complex numbers: According to Extended Pascal, GNU Pascal has
  1222.      built-in complex numbers and supports a number of mathematical
  1223.      functions on them, e.g. `Abs', `Sqr', `SqRt', `Exp', `Ln', `Sin',
  1224.      `Cos', `ArcTan'.
  1225.  
  1226.    * Record types: GNU Pascal by default aligns 32-bit fields on 4-byte
  1227.      addresses because this improves performance. So, e.g., the record
  1228.           type
  1229.             MyRec = record
  1230.               f, o, oo : Boolean;
  1231.               Bar : Integer
  1232.             end;
  1233.      has 8 bytes, not 7. Use the `--pack-struct' option or declare the
  1234.      record as `packed' to force GPC to pack it to 7 bytes.  However,
  1235.      note that this produces somewhat less efficient code on the x86
  1236.      and far less efficient code on certain other processors. Packing
  1237.      records and arrays is mostly useful only when using large
  1238.      structures where memory usage is a real concern, or when reading
  1239.      or writing them from/to binary files where the exact layout
  1240.      matters.
  1241.  
  1242.  
  1243. 
  1244. File: gpc.info,  Node: BP Procedural Types,  Next: Files,  Prev: Data Types in BP and GPC,  Up: Borland Pascal
  1245.  
  1246. BP Procedural Types
  1247. ===================
  1248.  
  1249.    In addition to BP's procedural types, GNU Pascal has pointers to
  1250. procedures:
  1251.  
  1252.      type
  1253.        FuncPtr = ^function (Real) : Real;
  1254.  
  1255.    The differences between procedure pointers and procedural types are
  1256. only syntactical:
  1257.  
  1258.    * In the first case, one can pass/assign a procedure/function with
  1259.      `@myproc', in the latter case just with `myproc' (which can lead
  1260.      to confusion in the case of functions - though GPC should always
  1261.      recognize the situation and not try to call the function).
  1262.  
  1263.    * In the first case, one can call the routine via `myprocptr^', in
  1264.      the latter case just with `myprocvar'.
  1265.  
  1266.    * To retrieve the address of a procedure stored in a variable, one
  1267.      can use `myprocptr' in the first case and `@myprocvar' in the
  1268.      latter.
  1269.  
  1270.    * If, for some reason, one needs the address of the variable itself,
  1271.      in the first case, that's obtained with `@myprocptr', in the
  1272.      second case with `@@myprocvar'!
  1273.  
  1274.    * Bottom line: BP style procedural types are simpler to use in normal
  1275.      cases, but somewhat strange in the last example.
  1276.  
  1277.    One can use both kinds in the same program, of course, though it is
  1278. recommended to stick to one kind throughout to avoid maximum confusion.
  1279.  
  1280.    GNU Pascal also supports Standard Pascal's procedural parameters
  1281. (*note Special Parameters::).
  1282.  
  1283.    Furthermore, GNU Pascal allows you to call even local procedures
  1284. through procedural pointers, variables or parameters without reverting
  1285. to any dirty tricks (like assembler, which is necessary in BP).
  1286.  
  1287.    The differences between the various kinds of procedural types,
  1288. pointers and parameters are demonstrated in the demo program
  1289. `procvardemo.pas'. An example for calling local routines through
  1290. procedural parameters can be found in the demo program
  1291. `iteratordemo.pas'.
  1292.  
  1293.